#include <OsgClassDataSource/NodeOsg.h>
#include <iostream>
#include <osg/Group>
#include <NodeInfoContainer.h>
#include <ObjectInspectorVisitorChangeBackground.h>
NodeOsg::NodeOsg( QObject *parent /*= 0*/ ) : ObjectOsg(parent)
{
    m_object = new osg::Node;
    m_boolValue = true;
//    osg::Node* nodePtr = dynamic_cast<osg::Node*>(m_object.get());
//    nodePtr->setNodeMask(0x1);
    this->refreshInfoContainer();
}


NodeOsg::~NodeOsg()
{

}

void NodeOsg::copyFrom(EObject *objPtr)
{
    ObjectOsg::copyFrom(objPtr);
    NodeOsg* objOsgPtr = dynamic_cast<NodeOsg*>(objPtr);
    if(objOsgPtr == NULL)return;

    osg::CopyOp copy_op(osg::CopyOp::Options::DEEP_COPY_ALL);
    osg::ref_ptr<osg::Object> new_obj = copy_op(objOsgPtr->m_object);

    //replace original m_object
    m_object = new_obj;
    this->refreshInfoContainer();

    //add to the same parent
    osg::Node* new_node = dynamic_cast<osg::Node*>(new_obj.get());
    osg::Node* old_node = dynamic_cast<osg::Node*>(objOsgPtr->m_object.get());
    if(new_node != NULL && old_node != NULL)
    {
        osg::Group* group = old_node->getParent(0);
        if(group != NULL)
        {
            group->addChild(new_node);
        }
    }


}

EObject *NodeOsg::clone()
{
    NodeOsg* newObj = new NodeOsg();
    newObj->copyFrom(this);
    return newObj;

}

bool NodeOsg::getDisplay()
{
    return m_boolValue;
}

void NodeOsg::setDisplay(bool boolValue)
{
    m_boolValue = boolValue;


    if(boolValue)
    {
        ObjectInspectorVisitorChangeBackground visitor;
        visitor.setBackgroundBrush(QBrush(QColor(197,214,245)));
        visitor.traverse(this->getObjItem());

        osg::Node* nodePtr = dynamic_cast<osg::Node*>(m_object.get());
        if(nodePtr != NULL)
        {
            osg::Node::NodeMask oldMask = nodePtr->getNodeMask();
            oldMask = oldMask | 0x4;
            nodePtr->setNodeMask(oldMask);
        }

    }
    else
    {
        ObjectInspectorVisitorChangeBackground visitor;
        visitor.setBackgroundBrush(QBrush(QColor(65,65,65), Qt::BrushStyle::HorPattern));
        visitor.traverse(this->getObjItem());

        osg::Node* nodePtr = dynamic_cast<osg::Node*>(m_object.get());
        if(nodePtr != NULL)
        {
            osg::Node::NodeMask oldMask = nodePtr->getNodeMask();
            oldMask = oldMask & 0xfffffffb;
            nodePtr->setNodeMask(oldMask);
        }
    }
}

NodeOsg *NodeOsg::getParentNodeOsg()
{
    EObject* parentEObj = NULL;
    osg::Node* mNode = dynamic_cast<osg::Node*>(m_object.get());

    osg::Group* parentGroup = NULL;
    if(mNode->getParents().size() > 0)
        parentGroup = mNode->getParent(0);

    NodeInfoContainer* parentInfoContainer = NULL;
    if(parentGroup != NULL)//to avoid the parent node is outline(when the node is selected), here we need two if judge.
    {
        parentInfoContainer = dynamic_cast<NodeInfoContainer*>(parentGroup->getUserDataContainer());
        if(parentInfoContainer != NULL)
        {
            parentEObj = parentInfoContainer->getObjectInspectorItem()->getObjectPtr().data();

        }
        else
        {
            parentGroup = mNode->getParent(0)->getParent(0);
            if(parentGroup != NULL)
            {
                parentInfoContainer = dynamic_cast<NodeInfoContainer*>(parentGroup->getUserDataContainer());
                if(parentInfoContainer != NULL)
                {
                    parentEObj = parentInfoContainer->getObjectInspectorItem()->getObjectPtr().data();

                }
            }

        }
    }

    NodeOsg* parentNodeOsg = dynamic_cast<NodeOsg*>(parentEObj);
    return parentNodeOsg;
}




